﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace Eflong.KJFiles
{
    public class Kj143DevFiles : DevFile
    {
        private readonly Kj143AqFileEx[ ] AqFileExs;

        private readonly Kj143AqFileEx _aqFcFile = new Kj143AqFileEx
                                                       {
                                                           AqFile = new Kj143AqFile( Kj143FileTypes.AqFcFile ) ,
                                                           Type = "AQFC"
                                                       };

        private readonly Kj143AqFileEx _aqGxFile = new Kj143AqFileEx
                                                       {
                                                           AqFile = new Kj143AqFile( Kj143FileTypes.AqGxFile ) ,
                                                           Type = "AQGX"
                                                       };

        private readonly Kj143AqFileEx _aqKcFile = new Kj143AqFileEx
                                                       {
                                                           AqFile = new Kj143AqFile( Kj143FileTypes.AqKcFile ) ,
                                                           Type = "AQKC"
                                                       };

        private readonly Kj143AqFileEx _aqMcFile = new Kj143AqFileEx
                                                       {
                                                           AqFile = new Kj143AqFile( Kj143FileTypes.AqMcFile ) ,
                                                           Type = "AQMC"
                                                       };

        public Kj143AqJh AqJhFile = new Kj143AqJh( );

        public bool DefinitionsChanged;

        // = @"D:\data\精英联网数据\14052201001";

        public Dictionary<string , string> DicFileContent = new Dictionary<string , string>( );

        public Kj143DevFiles( )
        {
            Path = @"D:\data\精英联网数据\14052201001";
            AqFileExs = new[ ] { _aqFcFile , _aqMcFile , _aqKcFile , _aqGxFile };
            AqMcFile = _aqMcFile.AqFile;
            AqKcFile = _aqKcFile.AqFile;
        }

        public Kj143AqFile AqMcFile { get; set; }

        public Kj143AqFile AqKcFile { get; set; }

        public string Path { get; set; }

        public bool ParseFiles( )
        {
            bool changed = false;
            var sortedList = new SortedList( );
            foreach ( string s in Directory.GetFiles( Path ) )
            {
                sortedList.Add( s , s );
            }
            string file;
            DateTime tm;
            for ( int index = 0; index < sortedList.Count; index++ )
            {
                file = ( string )sortedList.GetByIndex( index );
                tm = File.GetLastWriteTime( file );

                foreach ( Kj143AqFileEx aqFileEx in AqFileExs )
                {
                    if ( file.Contains( aqFileEx.Type ) )
                    {
                        changed |= aqFileEx.ParseFile( file , Encoding );
                        break;
                    }
                }
            }

            //if (!sortedList.Contains("AQJH")) return;
            if ( changed )
            {
                DoAqJh( );
            }
            return changed;
        }

        public void DoAqJh( )
        {
            string file = Path + "\\AQJH";
            if ( File.Exists( file ) == false ) return;

            DateTime tm = File.GetLastWriteTime( file );

            if ( !DicFileContent.ContainsKey( file ) )
            {
                DicFileContent.Add( file , file );
                AqJhFile.ReadFile( file );
            }
            else
            {
                if ( DicFileContent[ file ] != file )
                {
                    DicFileContent[ file ] = file;
                    AqJhFile.ReadFile( file );
                }
            }
            if ( AqJhFile.Changed )
            {
                AqJhFile.Changed = false;
                AqJhFile.WriteFile( file );
            }
        }
    }

    internal class Kj143AqFileEx
    {
        internal Kj143AqFile AqFile;
        internal string FileName = string.Empty;
        internal string Type;

        internal bool ParseFile( string file , Encoding encoding )
        {
            if ( file.Equals( FileName ) )
            {
                return false;
            }

            if ( !string.IsNullOrEmpty( FileName ) )
            {
                File.Delete( FileName );
            }
            FileName = file;
            AqFile.ParseFile( file , encoding );

            return true;
        }
    }

    /// <summary>
    /// 143号文的实时数据文件
    /// </summary>
    public class Kj143RtData : RtData
    {
        private readonly Kj143AqFileEx aqBjFileEx = new Kj143AqFileEx
                                                        {
                                                            AqFile = new Kj143AqFile( Kj143FileTypes.AqBjFile ) ,
                                                            Type = "AQBJ"
                                                        };

        private readonly Kj143AqFileEx[ ] aqFileExs;

        private readonly Kj143AqFileEx aqFzFileEx = new Kj143AqFileEx
                                                        {
                                                            AqFile = new Kj143AqFile( Kj143FileTypes.AqFzFile ) ,
                                                            Type = "AQFZ"
                                                        };

        private readonly Kj143AqFileEx aqLjFileEx = new Kj143AqFileEx
                                                        {
                                                            AqFile = new Kj143AqFile( Kj143FileTypes.AqLjFile ) ,
                                                            Type = "AQLJ"
                                                        };

        private readonly Kj143AqFileEx aqSsFileEx = new Kj143AqFileEx
                                                        {
                                                            AqFile = new Kj143AqFile( Kj143FileTypes.AqSsFile ) ,
                                                            Type = "AQSS"
                                                        };

        public Kj143AqFile AqBjFile;

        public Kj143AqFile AqLjFile;
        public Kj143AqFile AqSsFile;

        public bool DatasChanged;

        // = @"D:\data\精英联网数据\14052201001";

        public Dictionary<string , DateTime> DicFileContent = new Dictionary<string , DateTime>( );

        public Kj143RtData( )
        {
            aqFileExs = new[ ] { aqSsFileEx , aqBjFileEx , aqLjFileEx , aqFzFileEx };
            AqSsFile = aqSsFileEx.AqFile;
            AqBjFile = aqBjFileEx.AqFile;
            AqLjFile = aqLjFileEx.AqFile;
        }

        public string Path { get; set; }

        public bool IsFileTimeChanged( )
        {
            string[ ] files = Directory.GetFiles( Path );
            foreach ( string file in files )
            {
                foreach ( Kj143AqFileEx aqFileEx in aqFileExs )
                {
                    if ( file.Contains( aqFileEx.Type ) && !file.Equals( aqFileEx.FileName ) )
                    {
                        return true;
                    }
                }
            }
            return false;
        }

        public bool ParseFiles( )
        {
            bool changed = false;
            var sortedList = new SortedList( );
            foreach ( string s in Directory.GetFiles( Path ) )
            {
                sortedList.Add( s , s );
            }
            string file;
            DateTime tm;
            for ( int index = 0; index < sortedList.Count; index++ )
            {
                file = ( string )sortedList.GetByIndex( index );
                tm = File.GetLastWriteTime( file );
                foreach ( Kj143AqFileEx aqFileEx in aqFileExs )
                {
                    if ( file.Contains( aqFileEx.Type ) )
                    {
                        changed |= aqFileEx.ParseFile( file , devFile.Encoding );
                        break;
                    }
                }
            }

            if ( changed )
            {
                ( ( Kj143DevFiles )devFile ).DoAqJh( );
            }
            return changed;
        }

        public bool ParseRealDataFiles( )
        {
            return false;
        }
    }

    public enum Kj143FileTypes
    {
        AqJhFile = 0 ,
        AqFcFile = 1 ,
        AqMcFile = 2 ,
        AqKcFile = 3 ,
        AqGxFile = 4 ,
        AqSsFile = 5 ,
        AqBjFile = 6 ,
        AqLjFile = 7 ,
        AqFzFile = 8 ,
    }

    public class Kj143AqFile
    {
        public const StringSplitOptions Options = new StringSplitOptions( );
        public static string[ ] EnterStrings = new string[ 1 ] { "\r\n" };
        public bool Changed;
        public Kj143FileTypes FileType = Kj143FileTypes.AqMcFile;

        public Kj143AqItem[ ] Items;
        private string _sContent;

        public Kj143AqFile( Kj143FileTypes fileType )
        {
            FileType = fileType;
        }

        public Kj143AqFile( )
        {
            FileType = Kj143FileTypes.AqMcFile;
        }

        public void ParseFile( string sFileName )
        {
            ParseFile( sFileName , Encoding.GetEncoding( "gb2312" ) );
        }

        public void ParseFile( string sFileName , Encoding encoding )
        {
            string sContent;

            //sContent = KJXX_Files.ReadFileContent(sFileName, Encoding.GetEncoding("gb2312"));
            // sContent = KJXX_Files.ReadFileContent(sFileName, Encoding.UTF8);
            sContent = KJXX_Files.ReadFileContent( sFileName , encoding );
            if ( _sContent == sContent ) return;

            _sContent = sContent;
            if ( string.IsNullOrEmpty( sContent ) )
            {
                Items = null;
            }
            else
            {
                string[ ] arr = sContent.Split( EnterStrings , Options );
                switch ( FileType )
                {
                    case Kj143FileTypes.AqFcFile:

                        Items = ( from s in arr
                                  let item = new Kj143AqFcItem( )
                                  let ss = s.Split( ';' )
                                  let ret = item.Parse( ss )
                                  where ret
                                  select item ).Cast<Kj143AqFcItem>( ).ToArray( );
                        break;

                    case Kj143FileTypes.AqMcFile:

                        Items = ( from s in arr
                                  let ss = s.Split( ';' )
                                  let item = new Kj143AqMcItem( )
                                  let ret = item.Parse( ss )
                                  where ret
                                  select item ).Cast<Kj143AqMcItem>( ).ToArray( );
                        break;

                    case Kj143FileTypes.AqKcFile:
                        Items = ( from s in arr
                                  let item = new Kj143AqKcItem( )
                                  let ss = s.Split( ';' )
                                  let ret = item.Parse( ss )
                                  where ret
                                  select item ).Cast<Kj143AqKcItem>( ).ToArray( );
                        break;

                    case Kj143FileTypes.AqGxFile:
                        Items = ( from s in arr
                                  let item = new Kj143AqGxItem( )
                                  let ss = s.Split( ';' )
                                  let ret = item.Parse( ss )
                                  where ret
                                  select item ).Cast<Kj143AqGxItem>( ).ToArray( );
                        break;

                    case Kj143FileTypes.AqSsFile:
                        Items = ( from s in arr
                                  let item = new Kj143AqSsItem( )
                                  let ss = s.Split( ';' )
                                  let ret = item.Parse( ss )
                                  where ret
                                  select item ).Cast<Kj143AqSsItem>( ).ToArray( );
                        break;

                    case Kj143FileTypes.AqBjFile:
                        Items = ( from s in arr
                                  let item = new Kj143AqBjItem( )
                                  let ss = s.Split( ';' )
                                  let ret = item.Parse( ss )
                                  where ret
                                  select item ).Cast<Kj143AqBjItem>( ).ToArray( );
                        break;
                }
            }
            Changed = true;
        }
    }

    public class Kj143AqItem
    {
        public virtual bool Parse( string[ ] arr )
        {
            return false;
        }

        /// <summary>
        ///  2010-06-02/14:26:04
        /// </summary>
        /// <param name="arr"></param>
        /// <param name="index"></param>
        /// <returns></returns>
        public static DateTime ParseTime( string[ ] arr , int index )
        {
            DateTime time;
            string s = arr[ index ].Replace( '/' , ' ' );
            return DateTime.TryParse( s , out time ) ? time : DateTime.Now;
        }
    }

    /// <summary>
    ///
    ///
    /// 14052202001;1;0
    ///
    /// </summary>
    public class Kj143AqJhItem : Kj143AqItem
    {
        /// <summary>
        /// 煤矿编号 0
        /// </summary>
        public string CmNo;

        /// <summary>
        /// 文件传输状态 2
        /// （取值0或1）
        ///
        /// 执法网系统在正确读取并处理3）步骤中数据提供方生成的实时文件、累计量、分钟数据以及异常报警等文件后，
        /// 将文件传输状态重新更新为0，表示可以接受新的数据文件
        ///
        /// </summary>
        public string FileTransState;

        /// <summary>
        /// 初始化状态 1
        ///
        /// 初始化状态为0时，表示该煤矿信息未初始化，
        /// 此时数据提供方需要优先同时传输分站参数文件、模拟量参数文件、开关量参数文件以及传感器关系参数文件
        ///
        /// 只有在初始化状态为1时，且文件传输状态为0时，
        /// 数据提供方可以进行分站参数、模拟量参数、开关量参数、传感器关系参数、实时数据、累计量、每5分钟数据以及异常报警等文件的传输，
        /// 当该批次（可能1个或多个）文件传输完成时，数据提供方将文件传输状态置为1
        ///
        ///
        /// 执法网系统正确读取1）步骤中数据提供方生成的分站参数文件、模拟量参数文件、开关量参数文件以及传感器关系参数文件
        /// 并完成初始化后，将该状态更新为1，此后该状态将不再变化
        /// </summary>
        public string InitState;

        public Kj143AqJhItem( string sNo )
        {
            CmNo = sNo;
            InitState = "0";
            FileTransState = "0";
        }

        public Kj143AqJhItem( )
        {
            new Kj143AqJhItem( "14000000001" );
        }

        public override bool Parse( string[ ] arr )
        {
            if ( arr == null || arr.Length < 3 )
            {
                return false;
            }
            CmNo = arr[ 0 ];

            InitState = arr[ 1 ];

            FileTransState = arr[ 2 ];

            return true;
        }

        public override string ToString( )
        {
            return string.Format( "{0};{1};{2}" , CmNo , InitState , FileTransState );
        }
    }

    public class Kj143AqJh
    {
        public Kj143AqJhItem AqJhItem;

        public bool Changed;

        private string _sContent;

        public void WriteFile( string sFileName )
        {
            if ( AqJhItem == null ) return;

            AqJhItem.InitState = "1";
            AqJhItem.FileTransState = "0";
            KJXX_Files.WriteFileContent( sFileName , AqJhItem + "\r\n" );
        }

        public void ReadFile( string sFileName )
        {
            string sContent = KJXX_Files.ReadFileContent( sFileName );
            if ( _sContent == sContent ) return;

            _sContent = sContent;
            if ( string.IsNullOrEmpty( sContent ) )
            {
                AqJhItem = null;
            }
            else
            {
                string[ ] arr = sContent.Split( ';' );
                AqJhItem = new Kj143AqJhItem( );
                AqJhItem.Parse( arr );
                Changed = true;
            }
        }
    }

    /// <summary>
    ///
    ///  晋城;阳城;前进煤矿;14052202001;0001;0001;M01;W01;1304回采面1号甲烷传感器;
    ///  晋城;阳城;前进煤矿;14052202001;0002;0005;K02;1304掘进工作面;掘进巷道;
    /// </summary>
    public class Kj143CommonItem : Kj143AqItem
    {
        #region Kj143AddupTypes enum

        public enum Kj143AddupTypes
        {
            未知 = 0 ,
            混量 = 混合量 ,
            混合量 = 1 ,
            纯量 = 瓦斯量 ,
            瓦斯量 = 2 ,
            日混量 = 混合量日累计 ,
            混合量日累计 = 3 ,
            日纯量 = 瓦斯量日累计 ,
            瓦斯量日累计 = 4 ,
            累计量 = 其他 ,
            其他 = 99
        }

        #endregion Kj143AddupTypes enum

        #region Kj143AnalogTypes enum

        public enum Kj143AnalogTypes
        {
            未知 = 0 ,
            甲烷 = 1 ,
            高浓甲烷 = 2 ,
            一氧化碳 = 3 ,
            CO = 3 ,
            风速 = 4 ,
            温度 = 5 ,
            风压 = 6 ,
            风量 = 7 ,
            瓦斯涌出量 = 8 ,
            负压 = 9 ,
            矿尘 = 粉尘 ,
            粉尘 = 10 ,
            煤位 = 11 ,
            电压 = 12 ,
            电流 = 13 ,
            功率 = 14 ,
            电量 = 电度 ,
            电度 = 15 ,
            模拟量 = 其他 ,
            其他 = 99
        }

        #endregion Kj143AnalogTypes enum

        #region Kj143DigitalTypes enum

        public enum Kj143DigitalTypes
        {
            供电 = 1 ,
            局扇 = 2 ,
            主扇 = 3 ,
            馈电 = 4 ,
            风门 = 5 ,
            风筒 = 6 ,
            烟雾 = 7 ,
            断电 = 8 ,
            闭锁 = 9 ,
            二级断电 = 10 ,
            其他 = 99
        }

        #endregion Kj143DigitalTypes enum

        #region Kj143FZStates enum

        /// <summary>
        /// 分站运行状态
        ///
        /// 0	分站正常, 1	分站故障, 9	其他
        ///
        /// </summary>
        public enum Kj143FZStates
        {
            分站正常 = 0 ,
            分站故障 = 1 ,
            其他 = 9 ,
        }

        #endregion Kj143FZStates enum

        private const StringSplitOptions Options = new StringSplitOptions( );
        private static readonly string[ ] Splits = new string[ 1 ] { ";" };

        /// <summary>
        /// 市名 0
        /// </summary>
        public string CityName;

        /// <summary>
        /// 煤矿编号 3
        /// </summary>
        public string CmNo;

        /// <summary>
        /// 煤矿名称 2
        /// </summary>
        public string CoalMineName;

        /// <summary>
        /// 县名 1
        /// </summary>
        public string CountyName;

        /// <summary>
        /// 分站编号 4
        /// </summary>
        public string FzNo;

        /// <summary>
        /// 监测地点
        /// </summary>
        public string Location;

        public int NSensorType;

        /// <summary>
        /// 传感器编号 5
        /// </summary>
        public string No;

        /// <summary>
        /// 断电区域
        /// </summary>
        public string PowerOffArea;

        public Kj143SensorTypes SensorType;

        /// <summary>
        /// 数据生成时间
        /// </summary>
        public DateTime Time;

        /// <summary>
        /// 传感器类型 6
        /// </summary>
        public string Type;

        /// <summary>
        /// 量程单位
        /// </summary>
        public string Unit;

        public override bool Parse( string[ ] arr )
        {
            if ( arr == null || arr.Length < 7 )
            {
                return false;
            }

            Kj143CommonItem item = this;
            item.CityName = arr[ 0 ];
            item.CountyName = arr[ 1 ];
            item.CoalMineName = arr[ 2 ];
            item.CmNo = arr[ 3 ];
            item.FzNo = arr[ 4 ];
            item.No = arr[ 5 ];
            item.Type = arr[ 6 ];

            return true;
        }

        public static bool Parse( string str , out Kj143CommonItem item )
        {
            if ( string.IsNullOrEmpty( str ) )
            {
                item = null;
                return false;
            }

            string[ ] arr = str.Trim( ).Split( Splits , 18 , Options );

            bool ret = false;

            switch ( arr[ 6 ][ 0 ] )
            {
                case 'M':
                    var item1 = new Kj143AqMcItem( );
                    ret = item1.Parse( arr );
                    item = item1;
                    break;

                case 'K':
                    var item2 = new Kj143AqKcItem( );
                    ret = item2.Parse( arr );
                    item = item2;
                    break;

                case 'L':
                    var item3 = new Kj143AqMcItem( );
                    ret = item3.Parse( arr );
                    item = item3;
                    break;
                default:
                    item = new Kj143CommonItem( );
                    ret = item.Parse( arr );
                    break;
            }

            return ret;
        }

        /*
         *
         * M01	低浓度甲烷  M02	高低浓度甲烷  M03	CO  M04	风速  M05	温度
         * M06	风压  M07	风量  M08	瓦斯涌出量   M09	负压  M10	粉尘
         * M11	煤仓煤位    M12	电压  M13	电流  M14	电功率 M15	电度  M99	其他
         *
         */
    }

    public enum Kj143SensorTypes
    {
        Unkown = 0 ,
        Analog = 1 ,
        Digital = 2 ,
        Addup = 3 ,
        模拟量 = 1 ,
        开关量 = 2 ,
        累计量 = 3 ,
    }

    /// <summary>
    ///
    /// 传感器运行状态
    ///
    /// 0	正常, 1	报警, 2	断电, 3	超量程 , 4	传感器故障, 9	其他
    ///
    /// </summary>
    public enum Kj143SensorStates
    {
        正常 = 0 ,
        报警 = 1 ,
        断电 = 2 ,
        超量程 = 3 ,
        传感器故障 = 4 ,
        故障 = 9 ,
        其他 = 9
    }

    /// <summary>
    ///
    /// 模拟量测点项
    ///
    /// 晋城;阳城;前进煤矿;14052202001;0001;0001;M01;W01;1304回采面1号甲烷传感器;%;0.00;4.00;1.00;1.50;1.00;工作面及其回风巷;2007-06-03/15:23:30;;
    ///
    /// 晋城市;阳城县;煤矿1;14052201001;4208;0000004732;M01;W33;监测地点;%;0;4;1;1.5;1.5;断电地点;2010-06-02/14:26:04;;
    ///
    /// 晋城市;阳城县;煤矿1;14052201001;4208;0000002364;L01;W99;监测地点;m3;0;0;0;0;0; ;2010-06-02/14:26:04;;
    /// </summary>
    public class Kj143AqMcItem : Kj143CommonItem
    {
        public Kj143AddupTypes AddupType = Kj143AddupTypes.未知;

        /// <summary>
        /// 报警门限 12
        /// </summary>
        public float AlarmLimit;

        public Kj143AnalogTypes AnalogType = Kj143AnalogTypes.未知;

        /// <summary>
        /// 传感器设置地点编号  7
        /// </summary>
        public string AreaNo;

        /// <summary>
        /// 量程高值 11
        /// </summary>
        public float HighValue;

        /// <summary>
        /// 量程单位  9
        /// </summary>
        //public string Unit;
        /// <summary>
        /// 量程低值 10
        /// </summary>
        public float LowValue;

        /// <summary>
        /// 断电门限  13
        /// </summary>
        public float PowerOffLimit;

        /// <summary>
        /// 复电门限 14
        /// </summary>
        public float PowertOnLimit;

        public override bool Parse( string[ ] arr )
        {
            if ( arr == null || arr.Length < 17 )
            {
                return false;
            }

            base.Parse( arr );

            int n = 99;
            int.TryParse( arr[ 6 ].Substring( 1 , 2 ) , out n );

            if ( arr[ 6 ][ 0 ] == 'M' )
            {
                SensorType = Kj143SensorTypes.Analog;
                AnalogType = ( Kj143AnalogTypes )n;
            }
            else
            {
                SensorType = Kj143SensorTypes.Addup;
                AddupType = ( Kj143AddupTypes )n;
            }

            NSensorType = n;

            AreaNo = arr[ 7 ];

            Location = arr[ 8 ];

            Unit = arr[ 9 ];

            float v = 0;

            LowValue = float.TryParse( arr[ 10 ] , out v ) ? v : 0;

            HighValue = float.TryParse( arr[ 11 ] , out v ) ? v : 100;// 4;

            AlarmLimit = float.TryParse( arr[ 12 ] , out v ) ? v : 0;// 1;

            PowerOffLimit = float.TryParse( arr[ 13 ] , out v ) ? v : 0;// 1.5f;

            PowertOnLimit = float.TryParse( arr[ 14 ] , out v ) ? v : 0;// 1.0f;

            PowerOffArea = arr[ 15 ];

            Time = ParseTime( arr , 16 );
            return true;
        }
    }

    public class Kj143AqKcItem : Kj143CommonItem
    {
        public override bool Parse( string[ ] arr )
        {
            if ( arr == null || arr.Length < 10 )
            {
                return false;
            }

            base.Parse( arr );
            int n = 99;
            int.TryParse( arr[ 6 ].Substring( 1 , 2 ) , out n );
            SensorType = Kj143SensorTypes.Digital;
            NSensorType = n;

            Location = arr[ 7 ];

            PowerOffArea = arr[ 8 ];
            DateTime time;

            Time = DateTime.TryParse( arr[ 9 ] , out time ) ? time : DateTime.Now;
            return true;
        }
    }

    /// <summary>
    ///
    /// 晋城;阳城;前进煤矿;14052202001;0001;总回风;;
    ///
    /// </summary>
    public class Kj143AqFcItem : Kj143AqItem
    {
        /// <summary>
        /// 市名 0
        /// </summary>
        public string CityName;

        /// <summary>
        /// 煤矿编号 3
        /// </summary>
        public string CmNo;

        /// <summary>
        /// 煤矿名称 2
        /// </summary>
        public string CoalMineName;

        /// <summary>
        /// 县名 1
        /// </summary>
        public string CountyName;

        /// <summary>
        /// 分站编号 4
        /// </summary>
        public string FzNo;

        /// <summary>
        /// 分站位置
        /// </summary>
        public string Location;

        public override bool Parse( string[ ] arr )
        {
            if ( arr == null || arr.Length < 6 )
            {
                return false;
            }
            Kj143AqFcItem item = this;
            item.CityName = arr[ 0 ];
            item.CountyName = arr[ 1 ];
            item.CoalMineName = arr[ 2 ];
            item.CmNo = arr[ 3 ];
            item.FzNo = arr[ 4 ];
            item.Location = arr[ 5 ];
            return true;
        }
    }

    /// <summary>
    ///
    /// 晋城;阳城;前进煤矿;14052202001;0001;0015;0001;0012;1;2007-06-03/15:23:30;;
    ///
    /// </summary>
    public class Kj143AqGxItem : Kj143AqItem
    {
        #region GxTypes enum

        public enum GxTypes
        {
            控制 = 1 ,
            成对 = 2 ,
            主备 = 3 ,
        }

        #endregion GxTypes enum

        /// <summary>
        /// 市名 0
        /// </summary>
        public string CityName;

        /// <summary>
        /// 煤矿编号 3
        /// </summary>
        public string CmNo;

        /// <summary>
        /// 煤矿名称 2
        /// </summary>
        public string CoalMineName;

        /// <summary>
        /// 县名 1
        /// </summary>
        public string CountyName;

        /// <summary>
        /// 分站编号 4
        /// </summary>
        public string FzNo;

        /// <summary>
        /// 分站编号 6
        /// </summary>
        public string FzNo2;

        /// <summary>
        /// 关系类型（1位十进制数字，数字1代表控制关系，数字2代表成对关系，
        /// 例如：井下风门间关系，数字3代表主备关系，例如：主扇与备用主扇间的关系）
        /// </summary>
        public GxTypes GxType;

        /// <summary>
        /// 传感器编号 5
        /// </summary>
        public string No;

        /// <summary>
        /// 传感器编号 7
        /// </summary>
        public string No2;

        /// <summary>
        /// 数据生成时间  9
        /// </summary>
        public DateTime Time;

        public override bool Parse( string[ ] arr )
        {
            if ( arr == null || arr.Length < 10 )
            {
                return false;
            }
            Kj143AqGxItem item = this;
            item.CityName = arr[ 0 ];
            item.CountyName = arr[ 1 ];
            item.CoalMineName = arr[ 2 ];
            item.CmNo = arr[ 3 ];
            item.FzNo = arr[ 4 ];
            item.No = arr[ 5 ];
            item.FzNo2 = arr[ 6 ];
            item.No2 = arr[ 7 ];

            GxType = ( GxTypes )int.Parse( arr[ 8 ] );

            Time = ParseTime( arr , 9 );

            return true;
        }
    }

    ///
    ///晋城市;阳城县;煤矿1;14052201001;7223;0000001284;L03;监测地点;1649;2010-06-02/14:26:13;;
    ///
    ///
    public class Kj143AqLjItem : Kj143CommonItem
    {
        public Kj143AddupTypes AddupType;

        /// <summary>
        /// 量程高值 11
        /// </summary>
        public float HighValue;

        /// <summary>
        /// 量程低值 10
        /// </summary>
        public float LowValue;

        /// <summary>
        /// 监测值 8
        /// </summary>
        public float Value;

        public override bool Parse( string[ ] arr )
        {
            if ( arr == null || arr.Length < 10 )
            {
                return false;
            }
            base.Parse( arr );

            int n = 99;
            int.TryParse( arr[ 6 ].Substring( 1 , 2 ) , out n );
            SensorType = Kj143SensorTypes.Addup;
            NSensorType = n;
            AddupType = ( Kj143AddupTypes )n;
            Location = arr[ 7 ];

            ;
            float v = 0;
            Value = float.TryParse( arr[ 8 ] , out v ) ? v : 0;

            Time = ParseTime( arr , 9 );
            return true;
        }
    }

    /// <summary>
    /// 安全实时数据文件
    /// 实时数据文件：时间点+AQSS。例如：20080723065200AQSS
    ///
    /// 晋城市;阳城县;煤矿1;14052201001;4208;0000003277;M02;监测地点;0;1;1.23;2010-06-02/14:26:13;;
    /// 晋城市;阳城县;煤矿1;14052201001;4208;0000004740;M04;监测地点;0;1;75.62;2010-06-02/14:26:13;;
    ///
    ///
    /// 20070603152330AQSS文件内容如下
    ///晋城;阳城;前进煤矿;14052202001;0001;0001;M01;1304回采面1号甲烷传感器;0;0;0.2;2009-12-21/01:02:03;;
    ///晋城;阳城;前进煤矿;14052202001;0002;0011;M01;1307回采面11号甲烷传感器;0;1;1.1;2009-12-21/01:02:03;;
    ///晋城;阳城;前进煤矿;14052202001;0011;0012;M05;1411掘进面温度传感器;0;0;23.2;2009-12-21/01:02:03;;
    ///晋城;阳城;前进煤矿;14052202001;0009;0001;K03;主扇;0;0;1;2009-12-17/12:02:03;;
    ///晋城;阳城;前进煤矿;14052202001;0008;0001;K05;风门;0;1;1;2009-12-17/12:02:03;;
    ///
    ///
    ///
    /// </summary>
    public class Kj143AqSsItem : Kj143CommonItem
    {
        /// <summary>
        /// 分站运行状态
        /// </summary>
        public Kj143FZStates FzState;

        /// <summary>
        /// 传感器运行状态
        /// </summary>
        public Kj143SensorStates State;

        /// <summary>
        /// 传感器监测值
        /// </summary>
        public float Value;

        public override bool Parse( string[ ] arr )
        {
            if ( arr == null || arr.Length < 12 )
            {
                return false;
            }

            base.Parse( arr );

            Location = arr[ 7 ];

            ParseStates( arr );

            float v = 0;
            Value = float.TryParse( arr[ 10 ] , out v ) ? v : 0;

            Time = ParseTime( arr , 11 );
            return true;
        }

        public void ParseStates( string[ ] arr )
        {
            Location = arr[ 7 ];
            int n = 0;

            FzState = ( Kj143FZStates )( int.TryParse( arr[ 8 ] , out n ) ? n : 9 );

            State = ( Kj143SensorStates )( int.TryParse( arr[ 9 ] , out n ) ? n : 9 );
        }
    }

    /// <summary>
    /// 异常报警文件
    ///
    /// 晋城;阳城;前进煤矿;14052202001;0002;0011;M01;1307回采面11号甲烷传感器;0;1;模拟量;报警;2009-12-21/01:02:03;;
    ///
    /// </summary>
    public class Kj143AqBjItem : Kj143AqSsItem
    {
        public string SAlarm;
        public string SType;

        public override bool Parse( string[ ] arr )
        {
            if ( arr == null || arr.Length < 13 )
            {
                return false;
            }

            base.Parse( arr );

            SType = arr[ 10 ];

            SAlarm = arr[ 11 ];

            Time = ParseTime( arr , 12 );

            return true;
        }
    }

    public class Kj143Files
    {
        private const StringSplitOptions Options = new StringSplitOptions( );
        private static readonly string[ ] EnterStrings = new string[ 1 ] { "\r\n" };

        /// <summary>
        /// 模拟量测点
        /// </summary>
        public Kj143AqMcItem[ ] AnalogItems;

        public bool AqBjChanged;

        public bool AqGxChanged;
        public bool AqKcChanged;
        public bool AqMcChanged;

        public bool AqSsChanged;

        /// <summary>
        /// 报警测点
        /// </summary>
        public Kj143AqBjItem[ ] BjItems;

        /// <summary>
        /// 开关量测点
        /// </summary>
        public Kj143AqKcItem[ ] DigitalItems;

        /// <summary>
        /// 关系定义
        /// </summary>
        public Kj143AqGxItem[ ] GxItems;

        /// <summary>
        /// 实时数据测点
        /// </summary>
        public Kj143AqSsItem[ ] SsItems;

        private string _sAqBjContent;
        private string _sAqGxContent;
        private string _sAqKcContent;
        private string _sAqMcContent;
        private string _sAqSsContent;

        /// <summary>
        /// 解析模拟量测点文件
        /// </summary>
        /// <param name="sFileName"></param>
        public void ParseAqMcFile( string sFileName )
        {
            string sContent = KJXX_Files.ReadFileContent( sFileName );
            if ( _sAqMcContent == sContent ) return;

            _sAqMcContent = sContent;
            if ( string.IsNullOrEmpty( sContent ) )
            {
                AnalogItems = null;
            }
            else
            {
                string[ ] arr = sContent.Split( EnterStrings , Options );
                ParseAqMc( arr );
            }
            AqMcChanged = true;
        }

        /// <summary>
        /// 解析开关量测点文件
        /// </summary>
        /// <param name="sFileName"></param>
        public void ParseAqKcFile( string sFileName )
        {
            string sContent = KJXX_Files.ReadFileContent( sFileName );
            if ( _sAqKcContent == sContent ) return;
            _sAqKcContent = sContent;
            if ( string.IsNullOrEmpty( sContent ) )
            {
                DigitalItems = null;
            }
            else
            {
                string[ ] arr = sContent.Split( EnterStrings , Options );
                ParseAqKc( arr );
            }
            AqKcChanged = true;
        }

        /// <summary>
        /// 解析关系测点文件
        /// </summary>
        /// <param name="sFileName"></param>
        public void ParseAqGxFile( string sFileName )
        {
            string sContent = KJXX_Files.ReadFileContent( sFileName );
            if ( _sAqGxContent == sContent ) return;
            _sAqGxContent = sContent;
            if ( string.IsNullOrEmpty( sContent ) )
            {
                GxItems = null;
            }
            else
            {
                string[ ] arr = sContent.Split( EnterStrings , Options );
                ParseAqGx( arr );
            }
            AqGxChanged = true;
        }

        /// <summary>
        /// 解析实时数据文件
        /// </summary>
        /// <param name="sFileName"></param>
        public void ParseAqScFile( string sFileName )
        {
            string sContent = KJXX_Files.ReadFileContent( sFileName );
            if ( _sAqSsContent == sContent ) return;
            _sAqSsContent = sContent;
            if ( string.IsNullOrEmpty( sContent ) )
            {
                SsItems = null;
            }
            else
            {
                string[ ] arr = sContent.Split( EnterStrings , Options );
                ParseAqSs( arr );
            }
            AqSsChanged = true;
        }

        /// 解析报警数据文件
        /// </summary>
        /// <param name="sFileName"></param>
        public void ParseAqBjFile( string sFileName )
        {
            string sContent = KJXX_Files.ReadFileContent( sFileName );
            if ( _sAqBjContent == sContent ) return;
            _sAqBjContent = sContent;
            if ( string.IsNullOrEmpty( sContent ) )
            {
                BjItems = null;
            }
            else
            {
                string[ ] arr = sContent.Split( EnterStrings , Options );
                ParseAqBj( arr );
            }
            AqBjChanged = true;
        }

        /// <summary>
        /// 解析模拟量测点定义
        /// </summary>
        /// <param name="arr"></param>
        public void ParseAqMc( string[ ] arr )
        {
            AnalogItems =
                ( from s in arr
                  let item = new Kj143AqMcItem( )
                  let ret = item.Parse( s.Split( ';' ) )
                  where ret
                  select item )
                    .Cast<Kj143AqMcItem>( ).ToArray( );
        }

        /// <summary>
        /// 解析开关量测点定义
        /// </summary>
        /// <param name="arr"></param>
        public void ParseAqKc( string[ ] arr )
        {
            DigitalItems =
                ( from s in arr
                  let item = new Kj143AqKcItem( )
                  let ret = item.Parse( s.Split( ';' ) )
                  where ret
                  select item )
                    .Cast<Kj143AqKcItem>( ).ToArray( );
        }

        /// <summary>
        /// 解析实时数据
        /// </summary>
        /// <param name="arr"></param>
        public void ParseAqSs( string[ ] arr )
        {
            SsItems =
                ( from s in arr
                  let item = new Kj143AqSsItem( )
                  let ret = item.Parse( s.Split( ';' ) )
                  where ret
                  select item )
                    .Cast<Kj143AqSsItem>( ).ToArray( );
        }

        /// <summary>
        /// 解析报警数据
        /// </summary>
        /// <param name="arr"></param>
        public void ParseAqBj( string[ ] arr )
        {
            BjItems =
                ( from s in arr
                  let item = new Kj143AqBjItem( )
                  let ret = item.Parse( s.Split( ';' ) )
                  where ret
                  select item )
                    .Cast<Kj143AqBjItem>( ).ToArray( );
        }

        /// <summary>
        /// 解析关系定义
        /// </summary>
        /// <param name="arr"></param>
        public void ParseAqGx( string[ ] arr )
        {
            GxItems = ( from s in arr
                        let item = new Kj143AqGxItem( )
                        let ss = s.Split( ';' )
                        let ret = item.Parse( ss )
                        where ret
                        select item ).Cast<Kj143AqGxItem>( ).ToArray( );
        }
    }
}